<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="退款确认单id" prop="tkqrId">
        <el-input
          v-model="queryParams.tkqrId"
          placeholder="请输入退款确认单id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款单编码" prop="bxCode">
        <el-input
          v-model="queryParams.bxCode"
          placeholder="请输入退款单编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款日期" prop="bxDate">
        <el-date-picker clearable
          v-model="queryParams.bxDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择退款日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="退款类型" prop="bxFkyy">
        <el-input
          v-model="queryParams.bxFkyy"
          placeholder="请输入退款类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="售后抵扣金额" prop="afterSaleDeductionAmount">
        <el-input
          v-model="queryParams.afterSaleDeductionAmount"
          placeholder="请输入售后抵扣金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="售后付款金额" prop="afterSalePaymentAmount">
        <el-input
          v-model="queryParams.afterSalePaymentAmount"
          placeholder="请输入售后付款金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="住来款金额" prop="incomingPaymentAmount">
        <el-input
          v-model="queryParams.incomingPaymentAmount"
          placeholder="请输入住来款金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退回收款金额" prop="refundReceivedAmount">
        <el-input
          v-model="queryParams.refundReceivedAmount"
          placeholder="请输入退回收款金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="大写金额" prop="amountInWords">
        <el-input
          v-model="queryParams.amountInWords"
          placeholder="请输入大写金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="售后时间" prop="afterSaleTime">
        <el-date-picker clearable
          v-model="queryParams.afterSaleTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择售后时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="提交人" prop="submitter">
        <el-input
          v-model="queryParams.submitter"
          placeholder="请输入提交人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提交人id" prop="submitterId">
        <el-input
          v-model="queryParams.submitterId"
          placeholder="请输入提交人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提交部门" prop="submissionDepartment">
        <el-input
          v-model="queryParams.submissionDepartment"
          placeholder="请输入提交部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提交部门id" prop="submissionDepartmentId">
        <el-input
          v-model="queryParams.submissionDepartmentId"
          placeholder="请输入提交部门id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="售后付款二维码" prop="afterSalePaymentQrCode">
        <el-input
          v-model="queryParams.afterSalePaymentQrCode"
          placeholder="请输入售后付款二维码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款凭证附件" prop="refundVoucherAttachment">
        <el-input
          v-model="queryParams.refundVoucherAttachment"
          placeholder="请输入退款凭证附件"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户编码" prop="customerCode">
        <el-input
          v-model="queryParams.customerCode"
          placeholder="请输入客户编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="确认退款" prop="confirmRefund">
        <el-input
          v-model="queryParams.confirmRefund"
          placeholder="请输入确认退款"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否需要同步" prop="isSynchronized">
        <el-input
          v-model="queryParams.isSynchronized"
          placeholder="请输入是否需要同步"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="确认人员" prop="confirmer">
        <el-input
          v-model="queryParams.confirmer"
          placeholder="请输入确认人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="确认人员id" prop="confirmerId">
        <el-input
          v-model="queryParams.confirmerId"
          placeholder="请输入确认人员id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="确认时间" prop="confirmationTime">
        <el-date-picker clearable
          v-model="queryParams.confirmationTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择确认时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="付款凭证附件" prop="paymentVoucherAttachment">
        <el-input
          v-model="queryParams.paymentVoucherAttachment"
          placeholder="请输入付款凭证附件"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="付款状态" prop="paymentStatus">
        <el-select v-model="queryParams.paymentStatus" placeholder="请选择付款状态" clearable>
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['fina:refund:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['fina:refund:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fina:refund:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="el-icon-refresh"
            size="mini"
            @click="handleSync"
            v-hasPermi="['fina:refund:order:sync']"
        >数据同步</el-button>
        </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['fina:refund:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="serialNum" />
      <el-table-column label="退款确认单id" align="center" prop="tkqrId" />
      <el-table-column label="退款单编码" align="center" prop="bxCode" />
      <el-table-column label="退款日期" align="center" prop="bxDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bxDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退款类型" align="center" prop="bxFkyy" />
      <el-table-column label="售后抵扣金额" align="center" prop="afterSaleDeductionAmount" />
      <el-table-column label="售后付款金额" align="center" prop="afterSalePaymentAmount" />
      <el-table-column label="住来款金额" align="center" prop="incomingPaymentAmount" />
      <el-table-column label="退回收款金额" align="center" prop="refundReceivedAmount" />
      <el-table-column label="大写金额" align="center" prop="amountInWords" />
      <el-table-column label="售后时间" align="center" prop="afterSaleTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.afterSaleTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="提交人" align="center" prop="submitter" />
      <el-table-column label="提交人id" align="center" prop="submitterId" />
      <el-table-column label="提交部门" align="center" prop="submissionDepartment" />
      <el-table-column label="提交部门id" align="center" prop="submissionDepartmentId" />
      <el-table-column label="售后付款二维码" align="center" prop="afterSalePaymentQrCode" />
      <el-table-column label="退款凭证附件" align="center" prop="refundVoucherAttachment" />
      <el-table-column label="客户编码" align="center" prop="customerCode" />
      <el-table-column label="客户名称" align="center" prop="customerName" />
      <el-table-column label="确认退款" align="center" prop="confirmRefund" />
      <el-table-column label="是否需要同步" align="center" prop="isSynchronized" />
      <el-table-column label="确认人员" align="center" prop="confirmer" />
      <el-table-column label="确认人员id" align="center" prop="confirmerId" />
      <el-table-column label="确认时间" align="center" prop="confirmationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.confirmationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="付款凭证附件" align="center" prop="paymentVoucherAttachment" />
      <el-table-column label="付款状态" align="center" prop="paymentStatus" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['fina:refund:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['fina:refund:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改退款确认单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="退款确认单id" prop="tkqrId">
          <el-input v-model="form.tkqrId" placeholder="请输入退款确认单id" />
        </el-form-item>
        <el-form-item label="退款单编码" prop="bxCode">
          <el-input v-model="form.bxCode" placeholder="请输入退款单编码" />
        </el-form-item>
        <el-form-item label="退款日期" prop="bxDate">
          <el-date-picker clearable
            v-model="form.bxDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择退款日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="退款类型" prop="bxFkyy">
          <el-input v-model="form.bxFkyy" placeholder="请输入退款类型" />
        </el-form-item>
        <el-form-item label="售后抵扣金额" prop="afterSaleDeductionAmount">
          <el-input v-model="form.afterSaleDeductionAmount" placeholder="请输入售后抵扣金额" />
        </el-form-item>
        <el-form-item label="售后付款金额" prop="afterSalePaymentAmount">
          <el-input v-model="form.afterSalePaymentAmount" placeholder="请输入售后付款金额" />
        </el-form-item>
        <el-form-item label="住来款金额" prop="incomingPaymentAmount">
          <el-input v-model="form.incomingPaymentAmount" placeholder="请输入住来款金额" />
        </el-form-item>
        <el-form-item label="退回收款金额" prop="refundReceivedAmount">
          <el-input v-model="form.refundReceivedAmount" placeholder="请输入退回收款金额" />
        </el-form-item>
        <el-form-item label="大写金额" prop="amountInWords">
          <el-input v-model="form.amountInWords" placeholder="请输入大写金额" />
        </el-form-item>
        <el-form-item label="售后时间" prop="afterSaleTime">
          <el-date-picker clearable
            v-model="form.afterSaleTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择售后时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="提交人" prop="submitter">
          <el-input v-model="form.submitter" placeholder="请输入提交人" />
        </el-form-item>
        <el-form-item label="提交人id" prop="submitterId">
          <el-input v-model="form.submitterId" placeholder="请输入提交人id" />
        </el-form-item>
        <el-form-item label="提交部门" prop="submissionDepartment">
          <el-input v-model="form.submissionDepartment" placeholder="请输入提交部门" />
        </el-form-item>
        <el-form-item label="提交部门id" prop="submissionDepartmentId">
          <el-input v-model="form.submissionDepartmentId" placeholder="请输入提交部门id" />
        </el-form-item>
        <el-form-item label="售后付款二维码" prop="afterSalePaymentQrCode">
          <el-input v-model="form.afterSalePaymentQrCode" placeholder="请输入售后付款二维码" />
        </el-form-item>
        <el-form-item label="退款凭证附件" prop="refundVoucherAttachment">
          <el-input v-model="form.refundVoucherAttachment" placeholder="请输入退款凭证附件" />
        </el-form-item>
        <el-form-item label="客户编码" prop="customerCode">
          <el-input v-model="form.customerCode" placeholder="请输入客户编码" />
        </el-form-item>
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="确认退款" prop="confirmRefund">
          <el-input v-model="form.confirmRefund" placeholder="请输入确认退款" />
        </el-form-item>
        <el-form-item label="是否需要同步" prop="isSynchronized">
          <el-input v-model="form.isSynchronized" placeholder="请输入是否需要同步" />
        </el-form-item>
        <el-form-item label="确认人员" prop="confirmer">
          <el-input v-model="form.confirmer" placeholder="请输入确认人员" />
        </el-form-item>
        <el-form-item label="确认人员id" prop="confirmerId">
          <el-input v-model="form.confirmerId" placeholder="请输入确认人员id" />
        </el-form-item>
        <el-form-item label="确认时间" prop="confirmationTime">
          <el-date-picker clearable
            v-model="form.confirmationTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择确认时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="付款凭证附件" prop="paymentVoucherAttachment">
          <el-input v-model="form.paymentVoucherAttachment" placeholder="请输入付款凭证附件" />
        </el-form-item>
        <el-form-item label="付款状态">
          <el-radio-group v-model="form.paymentStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder,refundSync } from "@/api/fina/finarefund";

export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 退款确认单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tkqrId: null,        bxCode: null,        bxDate: null,        bxFkyy: null,        afterSaleDeductionAmount: null,        afterSalePaymentAmount: null,        incomingPaymentAmount: null,        refundReceivedAmount: null,        amountInWords: null,        afterSaleTime: null,        submitter: null,        submitterId: null,        submissionDepartment: null,        submissionDepartmentId: null,        afterSalePaymentQrCode: null,        refundVoucherAttachment: null,        customerCode: null,        customerName: null,        confirmRefund: null,        isSynchronized: null,        confirmer: null,        confirmerId: null,        confirmationTime: null,        paymentVoucherAttachment: null,        paymentStatus: null,      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询退款确认单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        serialNum: null,        tkqrId: null,        bxCode: null,        bxDate: null,        bxFkyy: null,        afterSaleDeductionAmount: null,        afterSalePaymentAmount: null,        incomingPaymentAmount: null,        refundReceivedAmount: null,        amountInWords: null,        afterSaleTime: null,        remark: null,        submitter: null,        submitterId: null,        submissionDepartment: null,        submissionDepartmentId: null,        afterSalePaymentQrCode: null,        refundVoucherAttachment: null,        customerCode: null,        customerName: null,        confirmRefund: null,        isSynchronized: null,        confirmer: null,        confirmerId: null,        confirmationTime: null,        paymentVoucherAttachment: null,        paymentStatus: "0",        delFlag: null,        createBy: null,        createTime: null,        updateBy: null,        updateTime: null      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.serialNum)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加退款确认单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const serialNum = row.serialNum || this.ids
      getOrder(serialNum).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改退款确认单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.serialNum != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleSync() {
      if (this.loading) {
        this.$modal.message("正常进行中，请不要重复点击...");
        return;
      }
      this.loading = true;
      this.$modal.confirm('是否确认同步数据？').then(() => {
        refundSync().then(response => {
          if (response.code === 200) {
            this.$modal.msgSuccess("同步成功");
            this.getList();
          } else {
            this.$modal.msgError("同步失败");
          }
        }).catch(() => {
          this.$modal.msgError("同步失败");
        }).finally(() => {
          this.loading = false;
        });
      }).catch(() => {
        this.loading = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const serialNums = row.serialNum || this.ids;
      this.$modal.confirm('是否确认删除退款确认单编号为"' + serialNums + '"的数据项？').then(function() {
        return delOrder(serialNums);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('md/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
